package cn.duli.myvideo.controller;

import cn.duli.myvideo.pojo.Movie;
import cn.duli.myvideo.service.MovieService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@CrossOrigin
@RestController
@RequestMapping("/movie")
public class MovieController {

    @Resource
    private MovieService movieService;
    /**
     * 根据电影名称模糊分页查询电影列表
     * @return
     */
    @GetMapping("/queryLike")
    public PageInfo<Movie> queryLike(
            @RequestParam(value = "mName") String mName,
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Movie> pageInfo = new PageInfo<>(movieService.queryLike(mName));
        return pageInfo;
    }


    /**
     * 查看所有电影列表
     * @return
     */
    @GetMapping("/findAll")
    public PageInfo<Movie> findAll(
            @RequestParam(defaultValue = "1") int pageNo,
            @RequestParam(defaultValue = "2") int pageSize) {
        PageHelper.startPage(pageNo,pageSize);
        PageInfo<Movie> pageInfo = new PageInfo<>(movieService.findAll());
        return pageInfo;
    }

    /**
     * 根据id查询相关电影
     * @param mId
     * @return
     */
    @GetMapping("/findById/{mId}")
    public Movie findById(@PathVariable("mId") Integer mId){
        return movieService.findById(mId);
    }

    /**
     * 增加电影
     * @param movie
     */

    @PostMapping("/save")
    public void save(@RequestBody Movie movie){
        movieService.save(movie);
    }

    /**
     * 修改电影
     * @param movie
     */
    @RequestMapping("/update")
    public void update(@RequestBody Movie movie){
        movieService.update(movie);
    }

    /**
     * 根据id删除电影（修改status状态码为0，表示删除）
     * @param mId
     */
    @RequestMapping("/deleteById/{mId}")
    public void deleteById(@PathVariable("mId") Integer mId){
        movieService.deleteById(mId);
    }
}
